package com.my.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.domain.entity.Complain;
import com.my.domain.entity.Repair;
import com.my.domain.vo.ComplainVO;
import com.my.domain.vo.TempVO;
import com.my.enums.ResultEnum;
import com.my.mapper.ComplainMapper;
import com.my.result.Result;
import com.my.service.ComplainService;
import com.my.utils.AccountProfile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 投诉表(Complain)表服务实现类
 *
 * @author makejava
 * @since 2022-04-15 19:57:14
 */
@Service
public class ComplainServiceImpl extends ServiceImpl<ComplainMapper, Complain> implements ComplainService {

    @Resource
    ComplainMapper complainMapper;
    /*返回当前用户的投诉列表*/
    @Override
    public Result getComplainList(Integer currentPage) {
        System.out.println("================="+currentPage+"==========================");
        Subject subject = SecurityUtils.getSubject();
        AccountProfile currentUser = (AccountProfile)subject.getPrincipal();
        LambdaQueryWrapper<Complain> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Complain::getUserId,currentUser.getId());
        lambdaQueryWrapper.eq(Complain::getDelFlag,1);
        lambdaQueryWrapper.orderByDesc(Complain::getComplainTime);
        Page<Complain> page = new Page<>(currentPage,5);
        IPage pageData = page(page,lambdaQueryWrapper);
        return Result.okResult(pageData);
    }
    @Override
    public Result deleteComplainById(Long id) {

        if(id!=null){
            Complain complain = new Complain();
            complain.setId(id);
            complain.setDelFlag(0);
            complainMapper.updateById(complain);//虚假删除
        }
        else
            return Result.errorResult(ResultEnum.DELETE_FAIL);

        return Result.okResult(ResultEnum.DELETE_SUCCESS);
    }
    @Override
    public Result addComplain(Long id,String content ) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile currentUser = (AccountProfile)subject.getPrincipal();
        Complain complain = new Complain();

//        repair.setRepairType(temp.getRepairType());
//        repair.setRepairDetail(temp.getRepairDetail());
        complain.setComplainContent(content);

        if(getById(id)!=null){

            complain.setId(id);
            complainMapper.updateById(complain);
            return Result.okResult(ResultEnum.UPDATE_SUCCESS);
        }
        Date date = new Date(System.currentTimeMillis());
        complain.setComplainTime(date);
        complain.setComplainState(0);
        complain.setChargeId(1l);//先设置默认唯1，因为我不能分配
        complain.setUserId(currentUser.getId());//因为还没搞token获取不到当前用户，先默认
        save(complain);
        return Result.okResult(ResultEnum.ADD_SUCCESS);
    }
}
